$(function () {
    formObj.init();
});

var formObj = new function () {
    'use strict';
    return {
        form: null,
        iId: 0,
        init: function () {
            this.iId = xhTool.getQuery('iId');
            this.form = new xhForm($('#xh_form'));
            this.form.parse().bindRequired($('#bt_modal_confirm,parent.document'));

            var schoolArr = [{id: '0', text: '请选择'}];
            var classArr = [{id: '0', text: '请选择'}];
            $.each(SchoolList, function (id, value) {
                schoolArr.push({id: id, text: value});
            });
            $('[name=iSchoolId]').select2({
                data: schoolArr,
            });
            $('[name=sAdClsName]').select2({
                data: classArr,
            });

            var um = UE.getEditor('sContent', {
                autoHeightEnabled: true,
                serverUrl: baseUrl + 'webproblem/uploadImage',
                textarea: 'sContent',
                initialFrameWidth: '100%',
                imageScaleEnabled: false,
                toolbars: [[
                    'source', '| ', 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'strikethrough', '|', 'superscript', 'subscript', '|',
                    'forecolor', 'backcolor', '|', 'removeformat',
                    '|', 'insertorderedlist', 'insertunorderedlist', '|', 'selectall', 'cleardoc', 'paragraph' ,
                    '|', 'justifyleft', 'justifycenter', 'justifyright',
                    '|', 'insertimage', 'insertvideo', 'link', 'unlink',
                    '|', 'horizontal', 'preview ',
                ]],

                /* 上传图片配置项 */
                "imageActionName": "uploadImage", /* 执行上传图片的action名称 */
                "imageFieldName": "upfile", /* 提交的图片表单名称 */
                "imageMaxSize": 2048000, /* 上传大小限制，单位B */
                "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
                "imageCompressEnable": true, /* 是否压缩图片,默认是true */
                "imageCompressBorder": 1600, /* 图片压缩最长边限制 */
                "imageInsertAlign": "none", /* 插入的图片浮动方式 */
                "imageUrlPrefix": "", /* 图片访问路径前缀 */
                "imagePathFormat": "",

                /* 上传视频配置 */
                "videoActionName": "uploadImage", /* 执行上传视频的action名称 */
                "videoFieldName": "upfile", /* 提交的视频表单名称 */
                "videoPathFormat": "", /* 上传保存路径,可以自定义保存路径和文件名格式 */
                "videoUrlPrefix": "", /* 视频访问路径前缀 */
                "videoMaxSize": 102400000, /* 上传大小限制，单位B，默认100MB */
                "videoAllowFiles": [
                    ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
                    ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], /* 上传视频格式显示 */

            });
            if (this.iId) {
                if (webProblem.iSchoolId) {
                    $('[name=iSchoolId]').val(webProblem.iSchoolId).trigger('change');
                    this.funObj().getClassData(webProblem.iSchoolId);
                }
                $('#sTitle').val(webProblem.sTitle);
                um.ready(function () {
                    um.setContent(webProblem.sContent);
                });
            }
            this.listener();
        },
        listener: function () {
            var $this = this;
            // 提交申请单
            $('#bt_form_add').click(function () {
                $this.funObj().saveApply();
            });
            $('#bt_form_close').click(function () {
                window.top.xhTop.closeActiveTab();
            });
            $('[name=iSchoolId]').change(function () {
                var iSchoolId = $(this).val();
                $('[name=sAdClsName]').empty();
                if (iSchoolId != 0) {
                    $this.funObj().getClassData(iSchoolId);
                    // 获取当前学校对应的学生
                } else {
                    var classArr = [{id: '0', text: '请选择'}];
                    $('[name=sAdClsName]').select2({
                        data: classArr
                    })
                }
            });
        },
        funObj: function () {
            var $this = this;
            return {
                //保存按钮处理事件
                saveApply: function () {
                    if ($('#iSchoolId').val() == 0) {
                        $this.funObj().initeVal('学校不能为空');
                        return false;
                    }
                    if ($('#sAdClsName').val() == 0) {
                        $this.funObj().initeVal('班级不能为空');
                        return false;
                    }
                    if (!$('#sTitle').val()) {
                        $this.funObj().initeVal('标题不能为空');
                        return false;
                    }
                    $this.form.submit('webproblem/saveWebProblem', {}, function (res) {
                        if (xhExtra.checkError(res)) {
                            return false;
                        } else {
                            window.top.xhTop.showSuccess(res.msg);
                            window.top.xhTop.getFrameByName('网络问题').obj.webProList.reload();
                            window.top.xhTop.closeActiveTab();
                        }

                    }, {check: false});
                },
                //验证报错返回s
                initeVal: function (message) {
                    window.top.xhTop.showMessage({type: 'error', title: message});
                    xhExtra.setBtnNormal($('#bt_modal_confirm', window.top.document));
                },
                getClassData: function (iSchoolId) {
                    var url = 'webproblem/getClass';
                    xhExtra.ajaxRequest(url, {schoolId: iSchoolId}, {}, function (data) {
                        var calssArr = [{id: '0', text: '请选择'}];
                        $.each(data, function (id, value) {
                            calssArr.push({id: value, text: value});
                        });
                        $('[name=sAdClsName]').select2({
                            data: calssArr
                        });
                        if (webProblem.sAdClsName != '') {
                            $('[name=sAdClsName]').val(webProblem.sAdClsName).trigger('change');
                        }
                    });
                },

            }
        }
    }
};